#!/usr/bin/env perl

my $np=8;
my $fft_size=12288;
my $max_iters=540*10;
my @num_ffts=(1, 2, 4, 8, 12, 16, 32, 64,270, 540);
my $prog="bin/fft_bench";

my $jobname="fftw.$fft_size";

my $exestat="";
foreach my $a (@num_ffts)
{
  my $niters=int($max_iters/$a);
  $exestat=$exestat."mpirun -np \$\{NP\} $prog --s  $fft_size  --n $a --i $niters\n";
}

&create_script($jobname,$exestat,$np);

sub create_script
{
  #my ($jobid,$myexe,$myinp,$myout)=@_;
  my ($jobid,$myexe,$mpisize)=@_;
  my $file=$jobid.".np".$mpisize.".pbs";
  my $mp=$mpisize/8;
  my $ppn=8;
  if($mp<1) 
  {
    $mp=1;
    $ppn=$mpisize;
  }

open (HANDLE, ">$file");  # Open for output

print HANDLE <<MYSCRIPT;
#!/bin/tcsh
#PBS -l walltime=00:30:00
#PBS -l nodes=$mp:ppn=$ppn
#PBS -V
#PBS -N $jobid.np=$ppn

setenv MYTEMP \${PBS_JOBNAME}.`echo \$PBS_JOBID | cut -d'.' -f1`

cd \${PBS_O_WORKDIR}
setenv OMP_NUM_THREADS 1

# keep all of this together--mpdboot takes less time if the nodefile is 
# shorter and contains uniq node names
setenv NP `wc -l \${PBS_NODEFILE} | cut -d'/' -f1`
setenv MPDSNP `uniq \${PBS_NODEFILE} |wc -l| cut -d'/' -f1`
cat \${PBS_NODEFILE} | uniq > /tmp/mpd_nodefile_\${USER}_\$\$
setenv MPD_NODEFILE /tmp/mpd_nodefile_\${USER}_\$\$
mpdboot -v -n \${MPDSNP} -f \${MPD_NODEFILE}
mpdtrace -l
rm -f \${MPD_NODEFILE}
#
#

setenv MV2_SRQ_SIZE 4000
setenv MV2_ON_DEMAND_THRESHOLD 2100

$myexe

mpdallexit

MYSCRIPT
close (HANDLE);
}

